package BinarySearch;

public class _875_KokoEatingBananas {
    int[] piles;
    int H;
    public int minEatingSpeed(int[] piles, int H) {
        this.piles = piles;
        this.H = H;
        int min = 1;
        int max = 100000000;
        while (min < max) {
            int middle = (max + min) / 2;
            if (!isCandidate(middle)) {
                min = middle + 1;
            } else {
                max = middle;
            }
        }
        return min;
    }

    public boolean isCandidate(int val) {
        int times = 0;
        for (int pile : piles) {
            times += (pile - 1) / val + 1;
        }
        return times <= H;
    }


}
